package com.dc2013.followme;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.text.format.Time;
import android.util.Log;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.android.volley.Request.Method;
import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.dc2013.adapter.LikeAdapter;
import com.dc2013.util.MyPreferences;
import com.dc2013.util.System_Util;
import com.dc2013.util.URLString;

public class MainActivity extends SherlockActivity 
{
	String hashed_Device_Key;
	String accessToken;
	MyPreferences pref;
	RequestQueue queue;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		pref = new MyPreferences(MainActivity.this, "followme");
		if (pref.getMyPrefBoolean("isfirst", true)) {
			String device_Key = Secure.getString(getContentResolver(),
					Secure.ANDROID_ID);
			System_Util util = new System_Util(MainActivity.this);
			if (device_Key == null) {
				device_Key = "kimhyoseokbabo" + Time.getCurrentTimezone();
			}
			hashed_Device_Key = util.md5(device_Key);
			util.makeToast("test : " + hashed_Device_Key, Toast.LENGTH_LONG);

		} else {
			hashed_Device_Key = pref.getMyPrefString("Hashed_Device_Key",
					"null");
			
		}
		pref.putMyPrefString("Hashed_Device_Key", hashed_Device_Key);

		queue = Volley.newRequestQueue(this);

		StringRequest request = new StringRequest(Method.POST, URLString.MAIN
				+ URLString.AUTH, new Response.Listener<String>() {

			@Override
			public void onResponse(String response) {
				// TODO 자동 생성된 메소드 스텁
				try {
					JSONObject obj = new JSONObject(response);
					Log.d("HttpResult", response);
					int message = obj.getInt("Message");
					
					if (message==13||message==10) {
						accessToken = obj.getString("Access_token");
						Log.d("access_token", accessToken);
						System_Util util = new System_Util(MainActivity.this);
						if (accessToken.equals("null")) {
							util.makeToast("로그인이 실패했습니다", Toast.LENGTH_LONG);
							finish();
						} else {
							util.makeToast("로그인에 성공했습니다", Toast.LENGTH_LONG);
							Intent intent = new Intent(MainActivity.this,
									ListHome_Activity.class);
							pref.putMyPrefString("access_token", accessToken);
							
							intent.putExtra("access_token", accessToken);
							startActivity(intent);
						}
					}

				} catch (JSONException e) {
					// TODO 자동 생성된 catch 블록
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError arg0) {
				// TODO 자동 생성된 메소드 스텁
			}
		}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				// TODO 자동 생성된 메소드 스텁
				Map<String, String> params = new HashMap<String, String>();
				params.put("device_key", hashed_Device_Key);
				Log.d("map:", hashed_Device_Key);
				return params;
			}
		};
		
		queue.add(request);
		
	}

	
}
